/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

package com.huawei.housekeeper.service.impl;

import static com.huawei.housekeeper.common.constant.ConfigConstant.TENANT_MAP_NAME;

import com.huawei.housekeeper.common.utils.K8sUtil;
import com.huawei.housekeeper.service.ConfigService;

import io.kubernetes.client.openapi.models.V1ConfigMap;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 配置修改业务层
 *
 * @author Y86241604
 * @since 2022-06-21
 */
@Service
@Slf4j
public class ConfigServiceImpl implements ConfigService {

    @Value("${k8s.configName}")
    private String configName;

    @Autowired
    private K8sUtil k8sUtil;

    @Value("${spring.cloud.kubernetes.config.namespace}")
    private String namespace;

    /**
     * 更新所有service的configMap中tenantMap信息
     *
     * @param domain 租户名
     * @param tenantSchema 数据库名
     */
    @Override
    public void updateTenantMap(String domain, String tenantSchema) {
        V1ConfigMap v1ConfigMap = k8sUtil.getConfigMap(namespace, configName);
        v1ConfigMap.putDataItem(TENANT_MAP_NAME + "." + domain, tenantSchema);
        k8sUtil.updateConfigMap(v1ConfigMap, namespace, configName);
    }

}
